//
//  MAUserManager.m
//  MateAdv
//
//  Created by 茶 on 2023/9/20.
//

#import "CAUserManager.h"
#import <SAMKeychain/SAMKeychain.h>
#import <XYIAPKit/XYStoreiTunesReceiptVerifier.h>

#define kKeyChainService @"com.richpeople.camera"
#define kKeyChainAccount @"photoCount"

@implementation CAUserManager

- (BOOL)vipStatus {
    if ([self getVipProduct]) {
        BOOL isVip = [[XYStoreiTunesReceiptVerifier shareInstance] isSubscribedWithAutoRenewProduct:[self getVipProduct]];
        return isVip;
    } else {
        return NO;
    }
    
    
//    if (CAUserObj(kUserVipEndTime)) {
//        NSTimeInterval userTime = [CAUserObj(kUserVipEndTime) doubleValue];
//        NSTimeInterval today = [[NSDate date] timeIntervalSince1970];
//        if (today < userTime) {
//            return YES;
//        } return NO;
//    } else {
//        return NO;
//    }
}

- (NSString *)vipEndStr {
    if (self.vipStatus) {
        NSDate *date = [[XYStoreiTunesReceiptVerifier shareInstance] fetchExpiresDate:[self getVipProduct]];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd"];
        return [formatter stringFromDate:date];
    } else {
        return @"";
    }
}

//利用SAMKeyChain生成次数
- (NSString *)getPhotoCount {
    NSString *count = [SAMKeychain passwordForService:kKeyChainService account:kKeyChainAccount];
    if (!count) {
        count = @"0";
        [SAMKeychain setPassword:count forService:kKeyChainService account:kKeyChainAccount];
    }
    return count;
}

- (void)setPhotoCountWithValue:(NSInteger)value {
    [SAMKeychain setPassword:[NSString stringWithFormat:@"%ld", value] forService:kKeyChainService account:kKeyChainAccount];

}

//利用SAMKeyChain生成次数
- (NSString *)getVipProduct {
    NSString *product = [SAMKeychain passwordForService:kKeyChainService account:kVipProduct];
    return product;
}

- (void)setVipProduct:(NSString *)product {
    [SAMKeychain setPassword:product forService:kKeyChainService account:kVipProduct];

}


@end
